<?php
namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class AddressController extends  Controller{



    //获取地址
    public function SelectAddress(Request $request){
        if($request ->isMethod('post')){
            $user_id      = $request ->input('user_id');
            $data         = DB::table('address')
                          ->select('address_id','name','phone','province','address','start')
                          ->where('user_address',$user_id)
                          ->get();
            if ($data == true) {
                return json::json('success', '获取收货地址成功', $data);
            } else {
                return json::json('failure', '获取收货地址失败', json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }

    }

    //添加地址
    public function AddAddress(Request $request){
        if($request ->isMethod('post')){
            $user_id       = $request -> input('user_id');
            $nane          = $request ->input('name');
            $phone         = $request ->input('phone');
            $address       = $request ->input('address');
            $detailed_address = $request->input('province');
            $creation_time = date('Y-m-d H:i:s');
            $select        = DB::table('address')
                           ->select('address')
                           ->where('address',$address)
                           ->get();
            if($select == true){
                return json::json('failure', '新添加的收货地址存在', json::value(1));
            }else {
                $data = DB::table('address')
                    ->insert([
                        'user_address' => $user_id,
                        'name' => $nane,
                        'phone' => $phone,
                        'address' => $address,
                        'province' => $detailed_address,
                        'creation_time' => $creation_time
                    ]);
                if ($data == true) {
                    return json::json('success', '添加新的收货地址成功', json::value(1));
                } else {
                    return json::json('failure', '添加新的收货地址失败', json::value(1));
                }
            }
        }else{
            return json::json('101','提交方式错误','');
        }

    }

    //修改地址
    public function UpdateAddress(Request $request){
        if($request ->isMethod('post')){
            $address_id      = $request ->input('address_id');
            $nane          = $request ->input('name');
            $phone         = $request ->input('phone');
            $address       = $request ->input('address');
            $detailed_address = $request ->input('province');
            $data            = DB::table('address')
                              ->where('address_id',$address_id)
                              ->update([
                                  'name'    => $nane,
                                  'phone'   => $phone,
                                  'address' => $address,
                                  'province'=>$detailed_address
                              ]);
            if ($data == true) {
                return json::json('success', '修改收货地址成功', json::value(1));
            } else {
                return json::json('failure', '修改收货地址失败', json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //删除址

    public function DelAddress(Request $request){
        if($request ->isMethod('post')){
            $address_id      = $request ->input('address_id');
            $data            = DB::table('address')
                             ->where('address_id',$address_id)
                             ->delete();
            if ($data == true) {
                return json::json('success', '删除收货地址成功', json::value(1));
            } else {
                return json::json('failure', '删除收货地址失败', json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    public function DefaultAddress(Request $request){
        if($request ->isMethod('post')){
            $address_id       = $request ->input('address_id');
            $user_id          = $request ->input('user_id');
            $data             = DB::table('address')
                              ->where('address_id',$address_id)
                              ->where('user_address',$user_id)
                              ->update([
                                  'start' =>1
                              ]);
            if ($data == true) {
                DB::table('address')
                    ->where('address_id','!=',$address_id)
                    ->where('user_address',$user_id)
                    ->update([
                    'start'  =>0
                ]);
                return json::json('success', '默认地址设址成功', json::value(1));
            } else {
                return json::json('failure', '默认地址设址失败', json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //登录时默认收货地址
    public function AddressDefault(Request $request){
        if($request ->isMethod('post')){
            $user_id         = $request ->input('user_id');
            $data             = DB::table('address')
                ->where('user_address',$user_id)
                ->where('start',1)
                ->get();
            foreach ($data as $value){
                $da   = array('address_id'=>$value->address_id,'address' =>$value->province.$value->address);
            }
            if ($data == true) {
                return json::json('success', '获取默认地址成功', $da);
            } else {
                return json::json('failure', '获取默认地址失败', json::value(1));
            }
        }
    }


}
